/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ec.socialfun.datamodel;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author jorge
 */
public abstract class DataModelPadre<T>  extends LazyDataModel<T> {
    @Override
    public void setRowIndex(int rowIndex) {
        /*
         * The following is in ancestor (LazyDataModel):
         * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
         */
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        }
        else {
            super.setRowIndex(rowIndex % getPageSize());
        }
    }
    protected String obtenerUsuarioLogeado(){
        HttpServletRequest hp = obtenerHttpServletRequest();
        return hp.getUserPrincipal().getName();
    }

    private HttpServletRequest obtenerHttpServletRequest() {
        return (HttpServletRequest) obtenerInstancia().getExternalContext().getRequest();
    }

    private FacesContext obtenerInstancia() {
        return FacesContext.getCurrentInstance();
    }

    protected Object obtenerBean(String nombreBean) {
        FacesContext context = obtenerInstancia();
        return context.getELContext().getELResolver().getValue(context
                .getELContext(), null, nombreBean);

    }
    
    protected String obtenerValorRequest(String id){
        FacesContext facesContext = obtenerInstancia();
        return (String) facesContext.getExternalContext().getRequestParameterMap().get(id);
    }
}
